# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt,QSize

class TabBarPlus(QtGui.QTabBar):
    def __init__(self):
        QtGui.QTabBar.__init__(self)
        self.setStyleSheet("QTabBar::tab{width:400px;}")
        self.setExpanding(True)
    # end Constructor


    def tabSizeHint(self, index):

        if index == self.count() - 1:
            return  QSize(0, 0)
        else:
           #originalSizeHint = super(TabBarPlus, self).tabSizeHint(index)
           #minimumWidth = max(60, originalSizeHint.width())
           return  QSize(100, 25)


    # end tabSizeHint


class DesktopWorkspace(QtGui.QWidget):



    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)

        self.mdiArea = QtGui.QMdiArea()
        self.mdiArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.mdiArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.mdiArea.setOption(QtGui.QMdiArea.DontMaximizeSubWindowOnActivation)

        self.tabeBar = TabBarPlus()
        self.tabeBar.currentChanged.connect(self._tab_changed)
        layout = QtGui.QVBoxLayout(self)

        layout.addWidget(self.mdiArea)
        layout.addWidget(self.tabeBar)
        layout.setContentsMargins(2, 2, 2,2)
        #self.mdiArea.closeActiveSubWindow.connect(self._subWindows_close)



    def addSubWindow(self, widget):
        subwindow = self.mdiArea.addSubWindow(widget)
        self.tabeBar.addTab(subwindow.windowTitle())
        self.connect(widget, QtCore.SIGNAL('closed()'), self._subWindows_close)
        self.connect(widget, QtCore.SIGNAL('normaled()'), self._showAll_Normal)
        subwindow.show()
        self.mdiArea.tileSubWindows()

    @QtCore.pyqtSlot()
    def _showAll_Normal(self):
        _currentWindow = self.mdiArea.activeSubWindow()
        for _window in self.mdiArea.subWindowList():
            if _window.widget().isMaximized():
                _window.widget().showNormal()
        self.mdiArea.setActiveSubWindow(_currentWindow)

    @QtCore.pyqtSlot()
    def _subWindows_close(self):
        i = self.tabeBar.currentIndex()
        self.tabeBar.removeTab(i)

    @QtCore.pyqtSlot()
    def _tab_changed(self):
        i = self.tabeBar.currentIndex()
        if i == 0:  # 'Start' tab
            return None
        maximized = self.mdiArea.activeSubWindow().widget().isMaximized()
        _currentWindow = self.mdiArea.subWindowList()[i]
        if maximized:
            _currentWindow.showMaximized()
        self.mdiArea.setActiveSubWindow(_currentWindow)